<?
require_once(_FRAMEWORK."/core/core.php");

// Core Model
class core_model extends core
{
	private static $aDatabases;

    function __construct()
	{
        parent::__construct();
        
        if ( !is_object(self::$aDatabases) ) {
			self::$aDatabases = array();
		}
	}
	
	function __get($key) {
		switch ( $key ) {
			case "mysqli":
				$helper =& self::$aDatabases[$key];
				if ( ! is_object($helper) ) {
					$helper = $this->getConnectionMysqli();
					self::$aDatabases[$key] =& $helper;
				}
				
				return $helper;
			break;
			
			case "dynamodb":
				$helper =& self::$aDatabases[$key];
				if ( ! is_object($helper) ) {
					$helper = $this->getConnectionDynamoDB();
					self::$aDatabases[$key] =& $helper;
				}
				
				return $helper;
			break;
			
			case "mongo":
				$helper =& self::$aDatabases[$key];
				if ( ! is_object($helper) ) {
					$helper = $this->getConnectionMongo();
					self::$aDatabases[$key] =& $helper;
				}
				
				return $helper;
			break;
			
			default:
				return $this->$key;
		}
		
		return null;
	}
	
	private function getConnectionMysqlI() 
	{
		$helper = helper('mysqli');
		$helper->doConnect(_MYSQL_HOST, _MYSQL_USERNAME, _MYSQL_PASSWD, _MYSQL_DATABASE);
		return $helper;
	}

	private function getConnectionMongo() 
	{
        $aMongoConfigInfo = array();
        $aMongoConfigInfo['host'] = _MONGO_HOST;
        $aMongoConfigInfo['option'] = array(
            'timeout'   => _MONGO_TIMEOUT,
            'username'  => _MONGO_USERNAME,
            'password'  => _MONGO_PASSWD
        );

		$helper = helper('mongo');
		$helper->doConnect($aMongoConfigInfo);
		return $helper;
	}
	
	private function getConnectionDynamoDB() 
	{
		$config = array();
 		$config['dev'] = array(
			// Amazon Web Services Key. Found in the AWS Security Credentials. You can also pass
			// this value as the first parameter to a service constructor.
			'key' => _DYNAMO_KEY,
	
			// Amazon Web Services Secret Key. Found in the AWS Security Credentials. You can also
			// pass this value as the second parameter to a service constructor.
			'secret' => _DYNAMO_SECRET,
	
			// This option allows you to configure a preferred storage type to use for caching by
			// default. This can be changed later using the set_cache_config() method.
			//
			// Valid values are: `apc`, `xcache`, or a file system path such as `./cache` or
			// `/tmp/cache/`.
			//'default_cache_config' => 'cache/',
			//'default_cache_config' => './cache/',
			//'default_cache_config' => './application/cache/',
			'default_cache_config' => _DYNAMO_DEFAULT_CACHE_CONFIG,
			
			// Determines which Cerificate Authority file to use.
			//
			// A value of boolean `false` will use the Certificate Authority file available on the
			// system. A value of boolean `true` will use the Certificate Authority provided by the
			// SDK. Passing a file system path to a Certificate Authority file (chmodded to `0755`)
			// will use that.
			//
			// Leave this set to `false` if you're not sure.
			'certificate_authority' => _DYNAMO_CERITIFICATE_AUTHORITY,
			'canonical_id'=> _DYNAMO_CONONICAL_ID,
			'bucket_name' => _DYNAMO_BUCKET_NAME
		);
			
		$helper = helper('dynamodb');
		$helper->init( $config );
		return $helper;
	}

    /**
     * @brief 
     * @author hkkwon
     * @param                 |                         |        
     * @return                 |                         |        
     **/
     public function setActivity($oData, $sUserIdx) 
     {
        $this->mongo->Activity->save('user_'.$sUserIdx, $oData);
     }

    /**
     * @brief 
     * @author hkkwon
     * @param                 |                         |        
     * @return                 |                         |        
     **/
     public function getActivity($sUserIdx) 
     {
        $iLt = time()-parent::$iCacheExpireTime;
        return $this->mongo->Activity->find('user_'.$sUserIdx, 
            array(
                'where' => array(
                    'Group' => parent::$sGroup, 
                    'Method' => parent::$sMethod,
                    'CreateAt' => array('$gt' => $iLt)
                ),
                'fields' => array("Return" => true),
                'limit' => 1
            )
        );
     }
}

?>
